package com.chj.comp.service.demo1.controller;

import com.chj.comp.common.core.util.QRCodeUtil;
import com.chj.comp.common.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cuihui
 * @since 2023-02-16
 */
@RestController
@RequestMapping("/qrcode")
@Api(tags = "二维码工具类测试接口")
@Slf4j
public class QRCodeTestController {

    /**
     * 第一种情况：生成二维码并将图片保存到本地，返回图片地址
     * @return
     */
    @ApiOperation(value="生成二维码(文件)", notes="生成二维码")
    @GetMapping(value = "/generateFileQRCode")
    public Result<String> generateFileQRCode(@ApiParam(value = "二维码内容") @RequestParam(name = "content", required = false, defaultValue = "https://www.baidu.com") String content) {
        String address = QRCodeUtil.generateFileQRCode(content);
        return Result.ok(address, "生成成功");
    }

    /**
     * 第二种情况：将输出流转为base64字符串返回前台进行预览
     * @return
     */
    @ApiOperation(value="生成二维码(字符串)", notes="生成二维码")
    @GetMapping(value = "/generateStringQRCode")
    public Result<String> generateStringQRCode(@ApiParam(value = "二维码内容") @RequestParam(name = "content", required = false, defaultValue = "https://www.baidu.com") String content) {
        String base64 = null;
        try {
            base64 = QRCodeUtil.generateStringQRCode(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.ok(base64, "生成成功");
    }

    /**
     * 第三种情况：生成带logo二维码并将图片保存到本地，返回图片地址
     * @return
     */
    @ApiOperation(value="生成带logo二维码(文件)", notes="生成带logo二维码")
    @GetMapping(value = "/generateFileQRCodeWithLogo")
    public Result<String> generateFileQRCodeWithLogo(@ApiParam(value = "二维码内容") @RequestParam(name = "content", required = false, defaultValue = "https://www.baidu.com") String content) {
        String address = null;
        try {
            address = QRCodeUtil.generateFileQRCodeWithLogo(content);
            return Result.ok(address, "生成成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }

    }

    /**
     * 第四种情况：生成带顶部说明二维码并将图片保存到本地，返回图片地址
     * @return
     */
    @ApiOperation(value="生成带顶部说明二维码(文件)", notes="生成带顶部说明二维码")
    @GetMapping(value = "/generateFileQRCodeWithUpDesc")
    public Result<String> generateFileQRCodeWithUpDesc(@ApiParam(value = "二维码内容") @RequestParam(name = "content", required = false, defaultValue = "https://www.baidu.com") String content,
                                                       @ApiParam(value = "顶部说明内容") @RequestParam(name = "upDesc", required = false, defaultValue = "顶部说明内容") String upDesc) {
        String address = QRCodeUtil.generateFileQRCodeWithUpDesc(content, upDesc);
        return Result.ok(address, "生成成功");
    }

    /**
     * 第五种情况：生成带底部说明二维码并将图片保存到本地，返回图片地址
     * @return
     */
    @ApiOperation(value="生成带底部说明二维码(文件)", notes="生成带底部说明二维码")
    @GetMapping(value = "/generateFileQRCodeWithBottomDesc")
    public Result<String> generateFileQRCodeWithBottomDesc(@ApiParam(value = "二维码内容") @RequestParam(name = "content", required = false, defaultValue = "https://www.baidu.com") String content,
                                                           @ApiParam(value = "底部说明内容") @RequestParam(name = "bottomDesc", required = false, defaultValue = "底部说明内容") String bottomDesc) {
        String address = QRCodeUtil.generateFileQRCodeWithBottomDesc(content, bottomDesc);
        return Result.ok(address, "生成成功");
    }

    /**
     * 解析二维码
     * @return
     */
    @ApiOperation(value="解析二维码", notes="解析二维码")
    @PostMapping(value = "/decodeQRCode")
    public Result<String> decodeQRCode(@RequestParam(value = "file") MultipartFile file) {
        String s = null;
        try {
            s = QRCodeUtil.decodeQRCode(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.ok(s, "解析成功");
    }
}
